<?php

class DComContact extends CFormModel {

    public $summary;
    public $info;
    private $_fileName = 'contact.json';

    public function getAttribute($name) {
        return $this->$name;
    }

    public function makeFile($aVar) {
        $filePath = Yii::app()->params['components_dir'] . $this->_fileName;
        $content  = json_encode($aVar);

        return file_put_contents($filePath, $content);
    }

    public function getFile() {
        $filePath = Yii::app()->params['components_dir'] . $this->_fileName;
        
        if (! is_file($filePath)) {
            $this->initFile();
        }

        $content = file_get_contents($filePath);
        return json_decode($content, true);
    }

    public function getData() {
        $result = $this->getFile();

        foreach ($result as $key => $value) {
            $this->$key = $value;
        }

        return TRUE;
    }
    
    public function attributeLabels() {
        return array(
            'summary' => 'Thông tin liên hệ',
            'support' => 'Thông tin khác',
        );
    }

    public function getInfoAttribute() {
        return array(
            'map'      => 'Bản đồ',
            'fanpage'  => 'Fanpage',
            'youtube'  => 'Youtube',
            'skype'    => 'Skype',
            'twitter'  => 'Twitter',
            'facebook' => 'Facebook',
            'yahoo'    => 'Yahoo',
        );
    }

    private function initFile() {
        $filePath = Yii::app()->params['components_dir'] . $this->_fileName;
        return file_put_contents($filePath, '{"summary":"<p>\r\n\t<b>ABC Company<\/b><br \/>\r\n\tAddress<br \/>\r\n\tPhone: (08) 1234 5678&nbsp;\u2013 Fax: (08) 1234 5678<\/p>\r\n"}');
    }

}